package com.woniuxy.mqConsumer.service;

import com.woniuxy.mqConsumer.entity.Orders;
import com.woniuxy.mqConsumer.entity.Product;
import com.woniuxy.mqConsumer.entity.Room;
import com.woniuxy.mqConsumer.entity.RoomUser;
import com.woniuxy.mqConsumer.mapper.OrderMapper;
import com.woniuxy.mqConsumer.mapper.ProductMapper;
import com.woniuxy.mqConsumer.mapper.RoomMapper;
import com.woniuxy.mqConsumer.mapper.RoomUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RoomUserMapper roomUserMapper;
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    ProductMapper productMapper;

    public void updateOrderStatus(int ordersId) {
        Orders orders = orderMapper.selectByPrimaryKey(ordersId);
        Room room = roomMapper.selectByPrimaryKey(orders.getRoomid());
        RoomUser roomUser = roomUserMapper.selectByUserIdAndRoomId(orders.getBuyuserid(),orders.getRoomid());
        Product product = productMapper.selectByPrimaryKey(orders.getProductid());
        if(orders.getOrderstatus() == 0){
            //订单未付款扣除保证金,修改商品状态为未成交,房间信息改为流拍
            orders.setOrderstatus(2);
            orders.setFinishdate(new Date());
            orderMapper.updateByPrimaryKey(orders);
            /**
             * 没有主键无法使用tkmapper
             */
            roomUser.setStatus(2);
            roomUserMapper.updateByPrimaryKey(roomUser);


            product.setStatus(0);
            productMapper.updateByPrimaryKey(product);
            room.setStatus(3);
            roomMapper.updateByPrimaryKey(room);
            System.err.println("违约");
            //可以发送短信通知该用户违约，保证金已扣除

        }else {
            System.err.println("已付款");
        }
    }

}
